﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAppPlanProduccion.ServiceReference.AuthApi;
using Web.Framework;
using PlanProd.Contracts;
using WebAppPlanProduccion.ServiceReference.PlanProdAdmin;

using PlanProd.Contracts.enums;
using System.ServiceModel;
using PlanProd.Contracts.Validation;
using PlanProd.Contracts.Search;
using Web.Framework.Svc;

namespace WebAppPlanProduccion.Code
{
    public class WebServiceHelper
    {
        public ServiceOptions ServiceOptionsDefault
        {
            get;
            set;
        }

        public static bool Authenticate(int applicationId, int companyId, string userName, string password)
        {
            AuthApiClient client = new AuthApiClient();
            bool isAuthenticated = false;
            try
            {
                isAuthenticated = client.ValidateUser(applicationId, companyId, userName, password);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return isAuthenticated;
        }

        public static List<Company> GetCompanies(int applicationId)
        {
            AuthApiClient client = new AuthApiClient();
            List<Company> companies = new List<Company>();

            try
            {
                companies.AddRange(client.GetCompanies(applicationId));
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return companies;
        }

        public static List<string> GetOperationsForUser(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<string> operations = new List<string>();

            try
            {
                operations.AddRange(client.GetOperationsForUser(applicationId, companyId, userName));
                client.Close();
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return operations;
        }

        public static List<AdUser> GetUsers(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<AdUser> adUserList = new List<AdUser>();
            try
            {
                adUserList.AddRange(client.GetUsers(applicationId, companyId, userName));
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return adUserList;
        }

        public List<Report> GetReports(SearchReport search)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetReports(this.ServiceOptionsDefault, search);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public Report SaveReport(Report report)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveReport(this.ServiceOptionsDefault, report);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarReport(int reportId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarReport(this.ServiceOptionsDefault, reportId);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<ReportServer> GetReportServers(SearchReportServer search)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetReportServers(this.ServiceOptionsDefault, search);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public ReportServer SaveReportServer(ReportServer reportServer)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveReportServer(this.ServiceOptionsDefault, reportServer);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarReportServer(int reportServerId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarReportServer(this.ServiceOptionsDefault, reportServerId);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Temporada> GetTemporadas(int empresaId, string nombre, DateTime? desde, DateTime? hasta, bool? activa)
        {
            List<Temporada> temporadas = new List<Temporada>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                temporadas.AddRange(client.GetTemporadas(this.ServiceOptionsDefault, nombre, empresaId, desde, hasta, activa));
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return temporadas;
        }

        public Temporada GetTemporadaById(int temporadaId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetTemporadaById(this.ServiceOptionsDefault, temporadaId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public Temporada SaveTemporada(Temporada temporada)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveTemporada(this.ServiceOptionsDefault, temporada);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarTemporada(int temporadaId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarTemporada(this.ServiceOptionsDefault, temporadaId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<CausaDesvio> GetCausasDeDesvio(int empresaId, string nombreLike)
        {
            List<CausaDesvio> desvios = new List<CausaDesvio>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                desvios.AddRange(client.GetCausasDesvios(this.ServiceOptionsDefault, empresaId, nombreLike));
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return desvios;
        }

        public CausaDesvio GetCausasDeDesvioById(int causaDesvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetCausaDeDesvioById(this.ServiceOptionsDefault, causaDesvioId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public CausaDesvio SaveCausaDesvio(CausaDesvio causaDesvio)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveCausaDesvio(this.ServiceOptionsDefault, causaDesvio);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarDesvioCausa(int causaDesvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarDesvioCausa(this.ServiceOptionsDefault, causaDesvioId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Destino> GetDestinos(int empresaId, string nombreLike)
        {
            List<Destino> destinos = new List<Destino>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                destinos.AddRange(client.GetDestinos(this.ServiceOptionsDefault, empresaId, nombreLike));
                client.Close();                
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return destinos;
        }

        public Destino GetDestinoById(int destinoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetDestinoById(this.ServiceOptionsDefault, destinoId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public Destino SaveDestino(Destino destinoCurrent)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveDestino(this.ServiceOptionsDefault, destinoCurrent);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarDestino(int destinoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarDestino(this.ServiceOptionsDefault, destinoId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<PedidoComercial> GetPedidosComerciales(int? pedidoId, int? temporadaId, int? destinoId, DateTime? fechaDesde, DateTime? fechaHasta, string nombreLike)
        {
            List<PedidoComercial> pedidosComerciales = new List<PedidoComercial>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                pedidosComerciales.AddRange(client.GetPedidosComerciales(this.ServiceOptionsDefault, pedidoId, temporadaId, destinoId, fechaDesde, fechaHasta, nombreLike));
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return pedidosComerciales;
        }

        public void EliminarPedidoComercial(int pedidoComercialId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPedidoComercial(this.ServiceOptionsDefault, pedidoComercialId);
                client.Close();
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PedidoComercial SavePedidoComercial(PedidoComercial pedidoComercial)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SavePedidoComercial(this.ServiceOptionsDefault, pedidoComercial);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void UpdatePedidoComercialDetalle(List<PedidoComercialDetalle> pedidoComercialDetalleList)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePedidoComercialDetalle(this.ServiceOptionsDefault, pedidoComercialDetalleList);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PedidoComercial GetPedidoComercialById(int id)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetPedidoComercialById(this.ServiceOptionsDefault, id);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarPedidoComercialDetalle(int pedidoComercialDetalleId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPedidoComercialDetalle(this.ServiceOptionsDefault, pedidoComercialDetalleId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarPedidoComercialDetalleAll(int pedidoComercialId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPedidoComercialDetalleAll(this.ServiceOptionsDefault, pedidoComercialId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<ProductoValidation> ValidarProductos(List<ProductoValidation> productosAValidar)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.ValidarProductos(productosAValidar);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Proveedor> GetProveedores(ServiceOptions serviceOptions, SearchProveedorCriteria criteria)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetProveedores(serviceOptions, criteria);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<PresupuestoProduccion> GetPresupuestosDeProduccion(ServiceOptions searchOptions, int? presupuestoId, string nombreLike, int? temporadaId, int? destinoId, int? proveedorId, EstadoAprobado? estado, DateTime? fechaDesde, DateTime? fechaHasta)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetPresupuestoProduccion(searchOptions, presupuestoId, nombreLike, temporadaId, destinoId, proveedorId, estado, fechaDesde, fechaHasta);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }


        public void EliminarPresupuestoProduccion(int presupuestoProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPresupuestoProduccion(this.ServiceOptionsDefault, presupuestoProduccionId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void AprobarPresupuestoProduccion(int presupuestoProduccionId, int companyId, string userName, bool aprobado)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.AprobarPresupuestoProduccion(this.ServiceOptionsDefault, presupuestoProduccionId, companyId, userName, aprobado);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void UpdatePresupuestoProduccionDetalle(List<PresupuestoProduccionDetalleProd> detalles)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePresupuestoProduccionDetalle(this.ServiceOptionsDefault, detalles);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PresupuestoProduccion SavePresupuestoProduccion(PresupuestoProduccion presupuestoProduccion)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SavePresupuestoProduccion(this.ServiceOptionsDefault, presupuestoProduccion);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void SavePresupuestoProduccionList(List<PresupuestoProduccion> presupuestoProduccionList)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.SavePresupuestoProduccionList(this.ServiceOptionsDefault, presupuestoProduccionList);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PresupuestoProduccion GetPresupuestoProduccionById(int id)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetPresupuestoProduccionById(this.ServiceOptionsDefault, id);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Producto> GetProductosForPresupuesto(string codigoProducto, string descripcionProducto, int presupuestoProduccionId, int temporadaId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetProductosForPresupuesto(this.ServiceOptionsDefault, codigoProducto, descripcionProducto, presupuestoProduccionId, temporadaId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void UpdatePresupuestoProduccionItems(int presupuestoProduccionId, List<UpdateCantidadPlanProd> updates, List<int> deletedProductItems)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePresupuestoProduccionItems(this.ServiceOptionsDefault, presupuestoProduccionId, updates, deletedProductItems);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<PlanProduccion> GetPlanProduccion(ServiceOptions searchOptions, int? planProduccionId, string nombreLike, int? temporadaId, int? destinoId, int? proveedorId, DateTime? fechaDesde, DateTime? fechaHasta)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetPlanProduccion(searchOptions, planProduccionId, nombreLike, temporadaId, destinoId, proveedorId, fechaDesde, fechaHasta);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarPlanProduccion(int planProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarPlanProduccion(this.ServiceOptionsDefault, planProduccionId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void UpdatePlanProduccionDetalle(List<PlanProduccionDetalleProd> planProduccionDetalles)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePlanProduccionDetalle(this.ServiceOptionsDefault, planProduccionDetalles);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PlanProduccion SavePlanProduccion(PlanProduccion planProduccion)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SavePlanProduccion(this.ServiceOptionsDefault, planProduccion);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PlanProduccion GetPlanProduccionById(int id, List<int> detalleIds)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetPlanProduccionById(this.ServiceOptionsDefault, id, detalleIds);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public PlanProduccion GetPlanProduccionById(int id)
        {
            return GetPlanProduccionById(id, null);
        }

        public List<Producto> GetPossibleProductosForPlan(string filter, int planProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetPossibleProductosForPlan(this.ServiceOptionsDefault, filter, planProduccionId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Producto> GetProductosForPlan(string filter, int planProduccionId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetProductosForPlan(this.ServiceOptionsDefault, filter, planProduccionId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public SearchProductResult SearchProducts(ServiceOptions serviceOptions, SearchProductCriteria criteria)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SearchProducts(serviceOptions, criteria);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void UpdatePlanProduccionItems(int planProduccionId, List<UpdateCantidadPlanProd> updates, List<int> deletedProductItems)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.UpdatePlanProduccionItems(this.ServiceOptionsDefault, planProduccionId, updates, deletedProductItems);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public Desvio GetDesvioById(int desvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetDesvioById(this.ServiceOptionsDefault, desvioId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Desvio> GetDesvios(ServiceOptions options, int? id, int? planProduccionId, string producto, string color, TipoDesvio? tipoDesvio, EstadoCharAprobado? estadoAprobado, int? causaDesvio, int? destinoId, int? temporadaId, int? proveedorId, DateTime? fechaDesde, DateTime? fechaHasta, DateTime? fechaDesvioDesde, DateTime? fechaDesvioHasta)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetDesvios(options, id, planProduccionId, producto, color, tipoDesvio, estadoAprobado, causaDesvio, destinoId, temporadaId, proveedorId, fechaDesde, fechaHasta, fechaDesvioDesde, fechaDesvioHasta);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public Desvio GetDesvioByProductoDelPlanDeProduccion(int detalleProductoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetDesvioByProductoDelPlanDeProduccion(this.ServiceOptionsDefault, detalleProductoId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public Desvio SaveDesvio(Desvio desvio)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveDesvio(this.ServiceOptionsDefault, desvio);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Desvio> SaveDesvioMasivo(List<Desvio> desvios)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveDesvioMasivo(this.ServiceOptionsDefault, desvios);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<Desvio> SaveYAprobarDesvioMasivo(List<Desvio> desvios, string userName, EstadoCharAprobado aprobado)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveYAprobarDesvioMasivo(this.ServiceOptionsDefault, desvios, userName, aprobado);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarDesvio(int desvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarDesvio(this.ServiceOptionsDefault, desvioId);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void AprobarDesvio(int desvioId, string userName, EstadoCharAprobado aprobado)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.AprobarDesvio(this.ServiceOptionsDefault, desvioId, userName, aprobado);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<IngresoDeProducto> GetIngresoDeProducto(SearchIngresoDeProducto search)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetIngresoDeProducto(this.ServiceOptionsDefault, search);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public IngresoDeProducto GetIngresoDeProductoById(int ingresoProductoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetIngresoDeProductoById(this.ServiceOptionsDefault, ingresoProductoId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public void EliminarIngresoDeProducto(int ingresoProductoId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarIngresoDeProducto(this.ServiceOptionsDefault, ingresoProductoId);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public IngresoDeProducto SaveIngresoDeProducto(IngresoDeProducto ingresoDeProducto)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.SaveIngresoDeProducto(this.ServiceOptionsDefault, ingresoDeProducto);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<RelacionProductoEntity> GetRelacionProducto(RelacionProductoSearch search)
        {
            List<RelacionProductoEntity> relacionProducto = new List<RelacionProductoEntity>();
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetRelacionProducto(this.ServiceOptionsDefault, search);
                client.Close();
                relacionProducto.AddRange(t);
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return relacionProducto;
        }

        public void SaveRelacionProducto(RelacionProductoEntity entity)
        {            
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.SaveRelacionProducto(entity);
                client.Close();               
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public void EliminarRelacionProducto(RelacionProductoEntity entity)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.EliminarRelacionProducto(entity);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public List<string> GuardarIngresoDeProductosMasivo(List<IngresoDeProducto> ingresoDeProducto)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GuardarIngresoDeProductosMasivo(this.ServiceOptionsDefault, ingresoDeProducto);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }
        }

        public List<DesvioOrigen> GetDesvioOrigen(int desvioId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var lst = client.GetDesvioOrigen(this.ServiceOptionsDefault, desvioId);
                client.Close();
                return lst;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }

                throw ex;
            }
        }

        public void IncorporarDesvios(int planProdccionId, int tipoDesvioId, int causaDesvioId, List<DesvioUpdate> desviosUpdate)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                client.IncorporarDesvios(this.ServiceOptionsDefault, planProdccionId, tipoDesvioId, causaDesvioId, desviosUpdate);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }

                throw ex;
            }
        }

        public List<ReportingConfiguration> GetReportingConfiguration(string name)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.GetReportingConfiguration(name);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }

                throw ex;
            }
        }

        public List<Producto> ActualizarProductos(List<Producto> productos, int empresaId)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.ActualizarProductos(this.ServiceOptionsDefault, productos, empresaId);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }

                throw ex;
            }
        }        

        public List<Producto> ActualizarProductosMasivo(SearchProductCriteria criteria)
        {
            PlanProdAdminClient client = new PlanProdAdminClient();
            try
            {
                var t = client.ActualizarProductosMasivo(this.ServiceOptionsDefault, criteria);
                client.Close();
                return t;
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }

                throw ex;
            }
        }

    }
}